package message

import "strconv"

type Head map[string]string

type Message struct {
	Heads Head
	Body  []byte
}
func (h Head) Get(key string) string {
	val, ok := h[key]
	if ok {
		return val
	}
	return ""
}
func (m *Message)MessageToByte() []byte {
	data := make([]byte, 0)
	leng := strconv.Itoa(len(m.Body))
	m.Heads[LENGTH] = leng
	for key, value := range m.Heads {
		buf := []byte(key + ":" + value + "\r\n")
		data = append(data, buf...)
	}
	//headlength := []byte(LENGTH + ":" + leng + "\r\n")
	//data = append(data, headlength...)
	buf := []byte("\r\n")
	data = append(data, buf...)
	if len(m.Body) > 0 {
		data = append(data, m.Body...)
	}
	end := []byte("-\r\n-")
	data = append(data, end...)
	return data
}